package cn.kdlc.demo.recycler.activity;

import android.databinding.DataBindingUtil;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import cn.kdlc.demo.recycler.R;
import cn.kdlc.demo.recycler.adapter.UserAdapter;
import cn.kdlc.demo.recycler.databinding.MainBinding;
import cn.kdlc.demo.recycler.model.Header;
import cn.kdlc.demo.recycler.model.User;
import com.h6ah4i.android.widget.advrecyclerview.animator.GeneralItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredDefaultItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MainBinding binding;
    private RecyclerView recyclerView;
    private RecyclerViewDragDropManager dragDropManager;
    private RecyclerView.Adapter wrappedAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        recyclerView = binding.recyclerView;
    }

    private void initData() {

        dragDropManager = new RecyclerViewDragDropManager();
        dragDropManager.setDraggingItemShadowDrawable((NinePatchDrawable) ContextCompat.getDrawable(this, R.drawable.material_shadow_z3_mdpi));

        List<Header> headers = new ArrayList<Header>();
        Header header1 = new Header(10000, "正在发售");
        Header header2 = new Header(10001, "还款完成");
        Header header3 = new Header(10002, "募集完成");

        headers.add(0, header1);
        headers.add(1, header2);
        headers.add(2, header3);

        SparseArray<List<User>> items = new SparseArray<List<User>>();
        List<User> users1 = new ArrayList<User>();
        List<User> users2 = new ArrayList<User>();
        List<User> users3 = new ArrayList<User>();

        for (int i = 0; i < 10; i++) {
            User user = new User(i, "name" + i, 20 + i);
            users1.add(user);
        }

        for (int i = 10; i < 14; i++) {
            User user = new User(i, "name" + i, 20 + i);
            users2.add(user);
        }

        for (int i = 14; i < 25; i++) {
            User user = new User(i, "name" + i, 20 + i);
            users3.add(user);
        }

        items.put(0, users1);
        items.put(1, users2);
        items.put(2, users3);

        UserAdapter adapter = new UserAdapter(headers, items);
        wrappedAdapter = dragDropManager.createWrappedAdapter(adapter);

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(wrappedAdapter);

        GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();
        recyclerView.setItemAnimator(animator);

        dragDropManager.attachRecyclerView(recyclerView);
    }
}
